package com.hub.service.asset.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.util.StringUtil;
import com.hub.service.impl.BaseServiceImpl;
import com.hub.dto.asset.SearchSupplierInfoDto;
import com.hub.dto.asset.SupplierInfoDto;
import com.hub.entity.asset.AssetSupplierInfo;
import com.hub.frame.consts.ConstantUtil;
import com.hub.utils.ApiCodeEnum;
import com.hub.utils.ApiResultObject;
import com.hub.utils.PagingResultObject;
import com.hub.utils.enums.SupplierTypeEnum;
import com.hub.mapper.asset.SupplierInfoMapper;
import com.hub.model.asset.SupplierInfoModel;
import com.hub.service.asset.ISupplierInfoService;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hub
 */
@Service
public class SupplierInfoServiceImpl extends BaseServiceImpl<SupplierInfoMapper, AssetSupplierInfo> implements ISupplierInfoService {
    @Autowired
    private SupplierInfoMapper supplierInfoMapper;
    @Autowired
    private Mapper mapper;

    /**
     * 
     * 分页查询
     * @param queryModel
     * @return
     */
    public PagingResultObject getList(SearchSupplierInfoDto queryModel){
        queryModel.setPageIndex( queryModel.getPageIndex() > 0 ? queryModel.getPageIndex() : 1);
        queryModel.setPageSize(queryModel.getPageSize() > 0?queryModel.getPageSize():20);
        //查询条件
        QueryWrapper<AssetSupplierInfo> wrapper = new QueryWrapper<>();
        if(StringUtil.isNotEmpty(queryModel.getSupplierCode())){
            wrapper.like("supplier_code", queryModel.getSupplierCode());
        }
        if(StringUtil.isNotEmpty(queryModel.getSupplierName())){
            wrapper.like("supplier_name", queryModel.getSupplierName());
        }
        if(StringUtil.isNotEmpty(queryModel.getSupplierFullName())){
            wrapper.like("supplier_full_name", queryModel.getSupplierFullName());
        }
        if(queryModel.getSupplierType() > 0){
            wrapper.eq("supplier_type",queryModel.getSupplierType());
        }
        wrapper.eq("active_flg", ConstantUtil.YES);
        IPage<AssetSupplierInfo> result = baseMapper.selectPage(new Page<>(queryModel.getPageIndex(), queryModel.getPageSize()), wrapper);
        List<SupplierInfoModel> modelList = new ArrayList<>();
        for(AssetSupplierInfo entity : result.getRecords()){
            SupplierInfoModel model = mapper.map(entity,SupplierInfoModel.class);
            model.setSupplierTypeStr(SupplierTypeEnum.getDescription(entity.getSupplierType()));
            modelList.add(model);
        }
        return PagingResultObject.success(modelList,queryModel.getPageIndex(),queryModel.getPageSize(),result.getTotal());
    }

    /**
     * 
     * 自动生成编号
     * @return
     */
    private String autoGenerateCode(){
        //获取当前总数
        long total = supplierInfoMapper.getSupplierTotle();
        //生成新的编号，规则为supplier+当前数量+1
        String newCode = "supplier-" + String.format("%03d",total+1);
        return newCode;
    }

    /**
     * 
     * 新增供应商
     * @param addModel
     * @return
     */
    public ApiResultObject doAddSupplier(SupplierInfoDto addModel){
        //查询供应商编号、简称、全称是否存在
        QueryWrapper<AssetSupplierInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("supplier_code",addModel.getSupplierCode())
                .or().eq("supplier_name",addModel.getSupplierName())
                .or().eq("supplier_full_name",addModel.getSupplierFullName());
        wrapper.eq("active_flg", ConstantUtil.YES);
        if(this.count(wrapper) > 0){
            return ApiResultObject.failed("供应商编号、简称或全称已存在，无法保存,请重新输入！");
        }
//        int flag = this.supplierInfoMapper.existenceByCodeOrName(addModel);
//        if(flag > 0){
//            return ApiResultObject.success(ApiCodeEnum.FAILED.getCode(),"供应商编号、简称或全称已存在");
//        }
        AssetSupplierInfo entity = mapper.map(addModel, AssetSupplierInfo.class);
        //有效性，默认为有效
//        entity.setActiveFlg(ActiveFlgEnum.Effective.getCode());
//        //获取当前时间，时间戳格式
//        Date nowDate = new Date();
//        //创建时间
//        entity.setCreateTime(nowDate);
//        //创建人
//        entity.setCreateUser(AppContextHolder.getUserName());
//        //最后更新时间
//        entity.setModifyTime(nowDate);
//        //最后更新人
//        entity.setModifyUser(AppContextHolder.getUserName());
        //自动生成编号
//        entity.setSupplierCode(this.autoGenerateCode());
        //新增
//        int count = this.supplierInfoMapper.doAddSupplier(entity);
        if(this.save(entity)){
            return ApiResultObject.success(ApiCodeEnum.SUCCESS.getCode(),"添加成功！");
        }
        return ApiResultObject.failed(ApiCodeEnum.FAILED.getCode(),"添加失败！");
    }

    /**
     * 
     * 删除供应商
     * @param id
     * @return
     */
    public ApiResultObject doDeleteSupplier(long id){
//        SupplierInfoEntity entity = new SupplierInfoEntity();
//        entity.setId(id);
//        entity.setModifyTime(new Date());
//        entity.setModifyUser(AppContextHolder.getUserName());
//        entity.setActiveFlg(ActiveFlgEnum.Invalid.getCode());
//        int count = this.supplierInfoMapper.doDeleteSupplier(entity);
        if(id < 1){
            return ApiResultObject.failed("参数异常: 资产信息ID异常!");
        }
        if(this.removeById(id)){
            return ApiResultObject.success(ApiCodeEnum.SUCCESS.getCode(),"删除成功！");
        }
        return ApiResultObject.failed(ApiCodeEnum.FAILED.getCode(),"删除失败！");
    };

    /**
     * 
     * 修改供应商信息
     * @param modifyModel
     * @return
     */
    public ApiResultObject doModifySupplier(SupplierInfoDto modifyModel){
        //验证供应商简称或全称是否已存在
//        int flag = this.supplierInfoMapper.verifyExistenceCodeOrName(modifyModel);
//        if(flag > 0){
//            return ApiResultObject.failed(ApiCodeEnum.FAILED.getCode(),"该供应商简称或全称已存在！！");
//        }
        //查询供应商编号、简称、全称是否存在
        QueryWrapper<AssetSupplierInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("supplier_name",modifyModel.getSupplierName()).or().eq("supplier_full_name",modifyModel.getSupplierFullName());
        wrapper.ne("id",modifyModel.getId());
        wrapper.eq("active_flg", ConstantUtil.YES);
        if(this.count(wrapper) > 0){
            return ApiResultObject.failed("供应商简称或全称已存在，无法保存,请重新输入！");
        }
        AssetSupplierInfo entity = mapper.map(modifyModel, AssetSupplierInfo.class);
        entity.setActiveFlg(ConstantUtil.YES);
        if(this.updateById(entity)){
            return ApiResultObject.success(ApiCodeEnum.SUCCESS.getCode(),"修改成功！");
        }
        return ApiResultObject.failed(ApiCodeEnum.FAILED.getCode(),"修改失败！");
    }
}